package com.etooling.framework.service;

import com.google.gson.Gson;
import com.jdcloud.sdk.auth.CredentialsProvider;
import com.jdcloud.sdk.auth.StaticCredentialsProvider;
import com.jdcloud.sdk.http.HttpRequestConfig;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.service.sms.client.SmsClient;
import com.jdcloud.sdk.service.sms.model.BatchSendRequest;
import com.jdcloud.sdk.service.sms.model.BatchSendResponse;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("js_sms")
public class JdCloudSendSmsServiceImpl implements SendSmsService {

    @Override
    public String sendVerifyCodeSms(String mobile, String verifyCode) {
        JdCloudSendSmsServiceImpl.init();
        BatchSendRequest request = new BatchSendRequest();
        request.setRegionId(region);
        // 设置模板ID 应用管理-文本短信-短信模板 页面可以查看模板ID
        request.setTemplateId("mb_90256a037d2b4da08e5545694aace2c5");
        // 设置签名ID 应用管理-文本短信-短信签名 页面可以查看签名ID
        request.setSignId("qm_23a46cdd41a64884a5bd08ad09b6c177");
        // 设置下发手机号list
        List<String> phoneList = new ArrayList<>();
        phoneList.add(mobile);
        request.setPhoneList(phoneList);
        // 设置模板参数，非必传，如果模板中包含变量请填写对应参数，否则变量信息将不做替换。
        List<String> params = new ArrayList<>();
        params.add(verifyCode);
        request.setParams(params);
        BatchSendResponse response = smsClient.batchSend(request);
        return new Gson().toJson(response);
    }

    private static String region = "cn-north-1";
    private static SmsClient smsClient;

    public static void init() {
        /**
         * 普通用户ak/sk （应用管理-文本短信-概览 页面可以查看自己ak/sk）
         */
        // 请填写AccessKey ID
        String accessKeyId = "BF4963CF4D8BB139CB334118186CCB1E";
        // 请填写AccessKey Secret
        String secretAccessKey = "88E1B253565CB63C3D0F3B2BDC9BEC8C";
        CredentialsProvider credentialsProvider = new StaticCredentialsProvider(accessKeyId, secretAccessKey);
        smsClient = SmsClient.builder()
                .credentialsProvider(credentialsProvider)
                .httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTP).build()) //默认为HTTPS
                .build();
    }
}
